//
//  EFDeliveryAddressViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/4.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFDeliveryAddressViewController.h" 
#import "EFTitleAddressItem.h"
#import "EFCurAddressItem.h"
#import "EFCurAddressCell.h"
#import "iToast.h"
#import "EFSearchAddressViewController.h"
#import "EFUserLocationItem.h"
#import "EFMyDeliveryAddressViewItem.h"

@interface EFDeliveryAddressViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic,strong) UIView* topBgView;
@property(nonatomic,strong) UIView* topView;
//@property(nonatomic,strong) UIView* lineView;
//@property(nonatomic,strong) UILabel* cityLabel;
@property(nonatomic,strong) UILabel* addressLabel;
//@property(nonatomic,strong) UIImageView* moreIV;
@property(nonatomic,strong) UIImageView* searchIV;


@end

@implementation EFDeliveryAddressViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self initData];
    self.enableBackButton = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(instancetype)init{
    self = [super init];
    if (self) {
        self.title = @"选择收货地址";
        self.dataArray = [NSMutableArray array];
    }
    return self;
}

-(void)initView{
    EFTitleAddressItem* title1 = [[EFTitleAddressItem alloc]init];
    title1.name = @"当前地址";
    [self.dataArray addObject:title1];
    
    EFCurAddressItem* curAddress = [[EFCurAddressItem alloc]init];
    curAddress.location = APP_CONTEXT.locationManager.pinLocation.detailAddress;
    [self.dataArray addObject:curAddress];
    
    EFTitleAddressItem* title2 = [[EFTitleAddressItem alloc]init];
    title2.name = @"我的收货地址";
    [self.dataArray addObject:title2];
    
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.topBgView];
    [self.topBgView addSubview:self.topView];
//    [self.topView addSubview:self.cityLabel];
//    [self.topView addSubview:self.moreIV];
 //   [self.topView addSubview:self.lineView];
    [self.topView addSubview:self.searchIV];
    [self.topView addSubview:self.addressLabel];
    
}

-(void)initData{
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net addressList:^(NSInteger code, NSDictionary *info, NSString *message) {
        if (code == 0) {
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                for (NSDictionary *dic in array) {
                    EFMyDeliveryAddressViewItem *item = [EFMyDeliveryAddressViewItem yy_modelWithDictionary:dic];
                    item.hideEdit = true;
                    [weakSelf.dataArray addObject:item];
                }
                //增加到全局
                APP_CONTEXT.addressManager.addressArray = weakSelf.dataArray;
            }
            [weakSelf.tableView reloadData];
        }else{
            
        }
    }];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    if([item isKindOfClass:[EFMyDeliveryAddressViewItem class]]){
        EFMyDeliveryAddressViewItem *item = (EFMyDeliveryAddressViewItem*)self.dataArray[indexPath.row];
        [[NSNotificationCenter defaultCenter] postNotificationName:IC_CHANGE_LOCATION_ADDRESS_NOTIFICATION object:item];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    if([item isKindOfClass:[EFMyDeliveryAddressViewItem class]]){
        return 70.0f;
    }
    return item.viewHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    
    if([item isKindOfClass:[EFCurAddressItem class]]){
        ((EFCurAddressCell*)cell).relocateblock = ^(){
            EFCurAddressItem* curAddress = [[EFCurAddressItem alloc]init];
            curAddress.location = @"定位中...";
            [self.dataArray replaceObjectAtIndex:1 withObject:curAddress];
            [self.tableView reloadData];
            
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                EFCurAddressItem* curAddress = [[EFCurAddressItem alloc]init];
                curAddress.location = APP_CONTEXT.locationManager.pinLocation.detailAddress;
                [self.dataArray replaceObjectAtIndex:1 withObject:curAddress];
                [self.tableView reloadData];
            });
            
        };
    }
    return cell;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight - 54)];
        _tableView.top = self.topBgView.bottom;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

-(UIView*)topBgView{
    if(_topBgView == NULL){
        _topBgView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, kScreenWidth, 54)];
        _topBgView.backgroundColor = [UIColor whiteColor];
    }
    return _topBgView;
}

-(UIView*)topView{
    if(_topView == NULL){
        _topView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 28)];
        _topView.backgroundColor = RGBCOLOR(0xF5F9F9);
        _topView.left = 20;
        _topView.top = 12;
        _topView.userInteractionEnabled = YES;
    }
    return _topView;
}

//-(UILabel*)cityLabel{
//    if(_cityLabel == NULL){
//        _cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 32, 17)];
//        _cityLabel.left = 8;
//        _cityLabel.top = 6;
//        _cityLabel.backgroundColor = [UIColor clearColor];
//        _cityLabel.font = [UIFont systemFontOfSize:15];
//        _cityLabel.textColor = kBlack666;
//        [_cityLabel setText:@"北京"];
//    }
//    return _cityLabel;
//}
//
//-(UIImageView*)moreIV{
//    if(_moreIV == NULL){
//        _moreIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 6, 4)];
//        _moreIV.left = self.cityLabel.right  ;
//        _moreIV.top = 12;
//        [_moreIV setImage:IMG(@"content_icon_search_menu")];
//    }
//    return _moreIV;
//}

//-(UIView*)lineView{
//    if(_lineView == NULL){
//        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 16)];
//        _lineView.left = 5;
//        _lineView.top = 6;
//        _lineView.backgroundColor = RGBCOLOR(0xE7E7E7);
//    }
//    return _lineView;
//}

-(UIImageView*)searchIV{
    if(_searchIV == NULL){
        _searchIV = [[UIImageView alloc ]initWithFrame:CGRectMake(0, 0, 14, 14)];
        _searchIV.left = 5;//self.lineView.right +5 ;
        _searchIV.top = 7;
        [_searchIV setImage:IMG(@"congtent_icon_search")];
    }
    return _searchIV;
}

-(UILabel*)addressLabel{
    if(_addressLabel == NULL){
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 17)];
        _addressLabel.left = self.searchIV.right + 5;
        _addressLabel.top = 6;
        _addressLabel.backgroundColor = [UIColor clearColor];
        _addressLabel.font = [UIFont systemFontOfSize:15];
        _addressLabel.textColor = kBlack999;
        [_addressLabel setText:@"请输入您的收货地址"];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickAddress)];
        [_addressLabel addGestureRecognizer:tap];
        _addressLabel.userInteractionEnabled = YES;
        
    }
    return _addressLabel;
}

-(void)onClickAddress{
    EFSearchAddressViewController *VC = [[EFSearchAddressViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}


@end
